package com.inspectortime.admin;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.inspectortime.admin.AdminRepository;
import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.User;
import com.inspectortime.repository.Factory;
import com.inspectortime.service.billing.BillingService;
import com.inspectortime.util.DesLocalMachineEncrypter;

public class AdminViewDelegate {

	private Factory factory;
	
	private AdminRepository repository;
	
	private BillingService billingService;
	
	public void setBillingService(BillingService billingService) {
		this.billingService = billingService;
	}

	private static Log log = LogFactory.getLog(AdminViewDelegate.class);
	
	
	public ModelAndView adminhome(HttpServletRequest request, HttpServletResponse response) {
		List<InspectorCompany> companies = repository.findAllInspectorCompanies();
		ModelAndView mv = new ModelAndView("inspector/adminhome");
		mv.addObject("inspectorCompanies", companies);
		return mv;
	}
	
	public ModelAndView billing(HttpServletRequest request, HttpServletResponse response) {
		List<InspectorCompany> companies = repository.findAllInspectorCompanies();
		ModelAndView mv = new ModelAndView("inspector/billing");
		return mv;
	}
	
	public ModelAndView runBillingService(HttpServletRequest request, HttpServletResponse response) {
		List<InspectorCompany> companies = repository.findAllInspectorCompanies();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, Integer.valueOf(request.getParameter("year")));
		// calendar month is 0-11 so subtract 1
		cal.set(Calendar.MONTH, (Integer.valueOf(request.getParameter("month")) - 1));
		String message = billingService.createInvoicesForMonthOf(cal.getTime());
		ModelAndView mv = new ModelAndView("redirect:/inspector/billing.html?message=" + message);
		return mv;
	}
	
	
	public ModelAndView viewEmployee(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		User user = repository.loadUser(id);
		DesLocalMachineEncrypter encrypter = new DesLocalMachineEncrypter();
		String encryptedEmail = encrypter.encrypt(user.getEmail());
		ModelAndView mv = new ModelAndView("inspector/viewEmployee");
		mv.addObject("employee", user);
		mv.addObject("encryptedEmail", encryptedEmail);
		return mv;
	}
	
	public ModelAndView viewInspectorCompany(HttpServletRequest request, HttpServletResponse response) {
		String companyId = request.getParameter("id");
		InspectorCompany company = repository.loadInspectorCompany(companyId);
		ModelAndView mv = new ModelAndView("inspector/viewCompany");
		mv.addObject("company", company);
		return mv;
	}
	
	public ModelAndView deleteInspectorCompany1(HttpServletRequest request, HttpServletResponse response) {
		String companyId = request.getParameter("id");
		InspectorCompany company = repository.loadInspectorCompany(companyId);
		ModelAndView mv = new ModelAndView("inspector/deleteConfirm");
		mv.addObject("company", company);
		return mv;
	}
	
	public ModelAndView deleteInspectorCompany2(HttpServletRequest request, HttpServletResponse response) {
		String companyId = request.getParameter("id");
		String areYouSure = request.getParameter("areYouSure");
		if (!areYouSure.equalsIgnoreCase("delete")) {
			ModelAndView mv = new ModelAndView("redirect:/inspector/adminhome.html?message=Invalid code, company was not deleted");
			return mv;
		}
		InspectorCompany company = repository.loadInspectorCompany(companyId);
		repository.deleteInspectorCompany(company);
		repository.flush();
		log.debug("Deleted inspector company " + company.getShortName());
		ModelAndView mv = new ModelAndView("redirect:/inspector/adminhome.html?message=Deleted company " + company.getShortName());
		return mv;
	}
	
	public void setFactory(Factory factory) {
		this.factory = factory;
	}
	
	public void setRepository(AdminRepository repository) {
		this.repository = repository;
	}

	public AdminRepository getRepository() {
		return repository;
	}    
}
